'use strict';

module.exports = app => {
  const { STRING, INTEGER, BOOLEAN, DATE } = app.Sequelize;

  const Address = app.model.define('address', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    memberId: { type: INTEGER, allowNull: false, comment: '会员ID' },
    name: { type: STRING(50), allowNull: false, comment: '收货人姓名' },
    phone: { type: STRING(20), allowNull: false, comment: '收货人电话' },
    province: { type: STRING(50), allowNull: false, comment: '省份' },
    city: { type: STRING(50), allowNull: false, comment: '城市' },
    district: { type: STRING(50), allowNull: false, comment: '区县' },
    detail: { type: STRING(255), allowNull: false, comment: '详细地址' },
    isDefault: { type: BOOLEAN, defaultValue: false, comment: '是否默认地址' },
    createdAt: DATE,
    updatedAt: DATE,
  });

  Address.associate = function() {
    app.model.Address.belongsTo(app.model.Member, { foreignKey: 'memberId', as: 'member' });
  };

  return Address;
};
